package net.oschina.app.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import net.oschina.app.util.MApp;
import net.oschina.app.R;
import net.oschina.app.util.UIUtils;

import java.util.List;

/**
 * Created by YL on 2017/3/31.
 */

public abstract class BaseFragment extends Fragment {
    public boolean isReadData = false;//是否读到数据
    public boolean isNullData = false;//是否数据为空
    public boolean isLoadData = false;
    public FrameLayout mView;

    private View mProgressview;
    public View mErrorView;
    public View mEmptyView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = (FrameLayout) inflater.inflate(R.layout.base_fragment, null);
        mView.addView(initView());
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dynamic();
    }

    //动态加载数据
    private void dynamic() {
        //1 显示加载中的界面
        showProgress();
        //2 加载数据
        setData();

    }

    public void showProgress() {
        if (mProgressview == null) {
            mProgressview = View.inflate(UIUtils.getContext(), R.layout.pager_loading, null);
        }
        changeView(mProgressview);
        Log.d("mylog", "showProgress");
    }

    public void changeView(View emptyView) {
        if (mView != null) {
            mView.removeAllViews();
        }
        mView.addView(emptyView);
    }

    //失败的界面
    public void showError() {
        if (mErrorView == null) {
            mErrorView = View.inflate(UIUtils.getContext(), R.layout.pager_error, null);
        }
        mErrorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dynamic();
            }
        });
        changeView(mErrorView);
    }

    //空的界面
    public void showEmpty() {
        if (mEmptyView == null) {
            mEmptyView = View.inflate(UIUtils.getContext(), R.layout.pager_empty, null);
        }
        mEmptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dynamic();
            }
        });
        changeView(mEmptyView);
    }

    //加载数据成功的流程
    public void dealCommonSuccess(Object list) {
        if (list instanceof List) {
            List l = (List) list;
            isNullData = l.size() == 0;
        } else {
            isNullData = list == null;
        }
        isReadData = true;
        runOnUIThread();
    }
    // 加载数据失败的流程
    public void dealCommonFail(){
        isReadData=false;
    }

    public void runOnUIThread() {
        MApp.mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                isLoadData = true;
                if (isReadData) {
                    //读到数据
                    if (!isNullData) {
                        //加载到数据
                        showSuccess();
                    } else {
                        showEmpty();

                    }
                } else {
                    //没有读到数据
                    showError();
                }
            }
        }, 0);
    }

    protected abstract View initView();

    protected abstract void setData();

    protected abstract void showSuccess();
}
